import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Pokemon } from './pockemon';
import { P_Comment } from './comment';
// import { collection } from './collection';

import { PokemonService } from './pokemon.service';
import { Global } from 'src/global';
import { ChangeDetectorRef } from '@angular/core';
@Component({
  selector: 'app-pokemon',
  templateUrl: './pokemon.component.html',
  styleUrls: ['./pokemon.component.css'],
})
export class PokemonComponent implements OnInit {
  id: string = '';
  pokemon: Pokemon = new Pokemon();
  commentArr!: P_Comment[];

  user = Global.user;
  comt:P_Comment=new P_Comment();
  content:string='';
  // collection:collection=new collection();

  constructor(
    public activeRoute: ActivatedRoute,
    private pokemonService: PokemonService,
    private changeDetectorRef: ChangeDetectorRef
  ) {}
  ngOnInit(): void {

    this.activeRoute.queryParams.subscribe((res) => {
      this.id = res['id'];
      this.pokemonService.get_user(this.user.username).subscribe((res) => {
        if (res['status'] == 'ok') {
          this.user = res['data'];
          // console.log(this.pokemon)
        }
      });

      this.pokemonService.getpokemon(this.id).subscribe((res) => {
        if (res['status'] == 'ok') {
          this.pokemon = res['data'];
          // console.log(this.pokemon)
        }
      });
      // this.pokemonService.getcollection(this.id).subscribe(res=>{
      //   if (res['status'] == 'ok') {
      //     this.collection = res['data'];
      //     // console.log(this.commentArr)
      //     // console.log(this.commentArr.length)
      //   }
      // })
      this.pokemonService.getcommentArr(this.id).subscribe((res) => {
        if (res['status'] == 'ok') {
          this.commentArr = res['data'];
          // console.log(this.commentArr)
          // console.log(this.commentArr.length)
        }
      });
    });
  }
  // 评论
  deleteComment(cid: P_Comment) {
    this.pokemonService.deleteComment(cid.id).subscribe((res) => {
      this.pokemonService.getcommentArr(this.id).subscribe((res) => {
        if (res['status'] == 'ok') {
          this.commentArr = res['data'];
          // console.log(this.commentArr)
          // console.log(this.commentArr.length)
        }
      });
      this.changeDetectorRef.markForCheck();
      this.changeDetectorRef.detectChanges();
    });
  }
  submitComment() {
    // console.log(this.comt)
    this.comt.p_id = this.id;
    // comt.time=
    this.comt.user = this.user;
    this.comt.content=this.content
    this.comt.like_user=[]
    this.pokemonService.submitComment(this.comt).subscribe((res) => {
      this.pokemonService.getcommentArr(this.id).subscribe((res) => {
        if (res['status'] == 'ok') {
          this.commentArr = res['data'];
          this.content=""
          // console.log(this.commentArr)
          // console.log(this.commentArr.length)
        }
      });
      this.changeDetectorRef.markForCheck();
      this.changeDetectorRef.detectChanges();
    });
  }
  likeComment(cid: P_Comment){
    this.pokemonService.likeComment(cid.id,this.user.username).subscribe((res) => {
      this.pokemonService.getcommentArr(this.id).subscribe((res) => {
        if (res['status'] == 'ok') {
          this.commentArr = res['data'];
          // console.log(this.commentArr)
          // console.log(this.commentArr.length)
        }
      });


      this.changeDetectorRef.markForCheck();
      this.changeDetectorRef.detectChanges();
    });

  }
  dislikeComment(cid: P_Comment){

    this.pokemonService.dislikeComment(cid.id,this.user.username).subscribe((res) => {
      this.pokemonService.getcommentArr(this.id).subscribe((res) => {
        if (res['status'] == 'ok') {
          this.commentArr = res['data'];
          // console.log(this.commentArr)
          // console.log(this.commentArr.length)
        }
      });
      
      this.changeDetectorRef.markForCheck();
      this.changeDetectorRef.detectChanges();
    });
  }
  // 收藏
  DISCOLLECTION(){
    this.pokemonService.discollection(this.pokemon,this.user.username).subscribe((res) => {
      this.pokemonService.get_user(this.user.username).subscribe((res) => {
        if (res['status'] == 'ok') {
          this.user = res['data'];
          // console.log(this.pokemon)
        }
      });
      
      this.changeDetectorRef.markForCheck();
      this.changeDetectorRef.detectChanges();
    });

  }
  COLLECTION(){
    this.pokemonService.collection(this.pokemon,this.user.username).subscribe((res) => {
      this.pokemonService.get_user(this.user.username).subscribe((res) => {
        if (res['status'] == 'ok') {
          this.user = res['data'];
          // console.log(this.pokemon)
        }
      });
      
      this.changeDetectorRef.markForCheck();
      this.changeDetectorRef.detectChanges();
    });

  }
  have_value(arr:Array<any>,val:any):boolean{
    // console.log(arr)
    // console.log(val)
    for (let i in arr ){
      // console.log(i)
      if (arr[i]==val || arr[i].Id==val){
        // console.log(111)
        return true
      }
    }
    return false
  }
}
